package com.cardbaobao.cardbabyclient.model;

import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.ui.activity.TransactionDetailActivity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by Jiangzx on 2017/9/21 10:56.
 */

public class TransactionData {
    private Details transaction_details;

    public Details getTransaction_details() {
        return transaction_details;
    }

    public void setTransaction_details(Details transaction_details) {
        this.transaction_details = transaction_details;
    }

    public static class Details {
        /**
         * id_card_no : 420281199001120070
         * rate_id : c444a1b031bb4293b5a5eac50bddfa5a
         * user_name : 柯常荣
         * fee : 1
         * modify_time : 2017-09-19 11:44:32
         * mobile_no : 18694056446
         * remark :
         * type : 2
         * debit_card_id : bb4030e7db614c2e94861221e5bf6fd5
         * rate : 0.5
         * rate_channel_type : KQB010103
         * cvn :
         * id : cbb20170918155805150572148535413
         * credit_card_id :
         * state : 1
         * member_id : 5001556
         * create_time : 2017-09-18 15:57:22
         * credit_card_no :
         * expire :
         * debit_card_name : 招商银行
         * receive_amount : 9.85
         * credit_card_name :
         * debit_card_no : 6214831203306979
         * payable_amount : 10
         * transaction_no : ZFB58708_09181557210650305
         */

        private String id_card_no;
        private String rate_id;
        private String user_name;
        private String fee;
        private String modify_time;
        private String mobile_no;
        private String remark;
        private String type;
        private String debit_card_id;
        private String rate;
        private String rate_channel_type;
        private String cvn;
        private String id;
        private String credit_card_id;
        private String state;
        private String member_id;
        private String create_time;
        private String credit_card_no;
        private String expire;
        private String debit_card_name;
        private String receive_amount;
        private String credit_card_name;
        private String debit_card_no;
        private String payable_amount;
        private String transaction_no;

        public String getId_card_no() {
            return id_card_no;
        }

        public void setId_card_no(String id_card_no) {
            this.id_card_no = id_card_no;
        }

        public String getRate_id() {
            return rate_id;
        }

        public void setRate_id(String rate_id) {
            this.rate_id = rate_id;
        }

        public String getUser_name() {
            return user_name;
        }

        public void setUser_name(String user_name) {
            this.user_name = user_name;
        }

        public String getFee() {
            return fee;
        }

        public void setFee(String fee) {
            this.fee = fee;
        }

        public String getModify_time() {
            return modify_time;
        }

        public void setModify_time(String modify_time) {
            this.modify_time = modify_time;
        }

        public String getMobile_no() {
            return mobile_no;
        }

        public void setMobile_no(String mobile_no) {
            this.mobile_no = mobile_no;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getDebit_card_id() {
            return debit_card_id;
        }

        public void setDebit_card_id(String debit_card_id) {
            this.debit_card_id = debit_card_id;
        }

        public String getRate() {
            return rate;
        }

        public void setRate(String rate) {
            this.rate = rate;
        }

        public String getRate_channel_type() {
            return rate_channel_type;
        }

        public void setRate_channel_type(String rate_channel_type) {
            this.rate_channel_type = rate_channel_type;
        }

        public String getCvn() {
            return cvn;
        }

        public void setCvn(String cvn) {
            this.cvn = cvn;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getCredit_card_id() {
            return credit_card_id;
        }

        public void setCredit_card_id(String credit_card_id) {
            this.credit_card_id = credit_card_id;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getMember_id() {
            return member_id;
        }

        public void setMember_id(String member_id) {
            this.member_id = member_id;
        }

        public String getCreate_time() {
            return create_time;
        }

        public void setCreate_time(String create_time) {
            this.create_time = create_time;
        }

        public String getCredit_card_no() {
            return credit_card_no;
        }

        public void setCredit_card_no(String credit_card_no) {
            this.credit_card_no = credit_card_no;
        }

        public String getExpire() {
            return expire;
        }

        public void setExpire(String expire) {
            this.expire = expire;
        }

        public String getDebit_card_name() {
            return debit_card_name;
        }

        public void setDebit_card_name(String debit_card_name) {
            this.debit_card_name = debit_card_name;
        }

        public String getReceive_amount() {
            return receive_amount;
        }

        public void setReceive_amount(String receive_amount) {
            this.receive_amount = receive_amount;
        }

        public String getCredit_card_name() {
            return credit_card_name;
        }

        public void setCredit_card_name(String credit_card_name) {
            this.credit_card_name = credit_card_name;
        }

        public String getDebit_card_no() {
            return debit_card_no;
        }

        public void setDebit_card_no(String debit_card_no) {
            this.debit_card_no = debit_card_no;
        }

        public String getPayable_amount() {
            return payable_amount;
        }

        public void setPayable_amount(String payable_amount) {
            this.payable_amount = payable_amount;
        }

        public String getTransaction_no() {
            return transaction_no;
        }

        public void setTransaction_no(String transaction_no) {
            this.transaction_no = transaction_no;
        }
    }

    private static String getChannel(String channelType) {
        String channel = "";
        switch (channelType) {
            case Constant.KQB010101:
                channel = "快捷支付";
                break;
            case Constant.KQB010102:
                channel = "微信";
                break;
            case Constant.KQB010103:
                channel = "支付宝";
                break;
            case Constant.KQB010104:
                channel = "京东支付";
                break;
            case Constant.KQB010105:
                channel = "QQ支付";
                break;
        }
        return channel;
    }

    public static List<Bean> parse(Details details, int type) {
        List<Bean> list = new ArrayList<>();
        BigDecimal fee=new BigDecimal(details.getPayable_amount()).
                subtract(new BigDecimal(details.getReceive_amount()));
        if (type == TransactionDetailActivity.TYPE_DEBIT) {
            //收款详情
            if (Constant.KQB010101.equals(details.getRate_channel_type())) {
                //快捷收款
                list.add(new Bean("付款银行", details.getCredit_card_name()));
                String cardNumber = details.getCredit_card_no();
                StringBuilder cardNumberSB = new StringBuilder(Constant.CARD_PREFIX);
                int cardLength=cardNumber.length();
                for (int i = cardLength-4; i < cardLength; i++) {
                    cardNumberSB.append(cardNumber.charAt(i));
                }
                list.add(new Bean("付款账号", cardNumberSB.toString()));
                list.add(new Bean("交易时间", details.getCreate_time()));
                list.add(new Bean("交易类型", getChannel(details.getRate_channel_type())));
                list.add(new Bean("交易状态", convertStatus(details.getState())));

                list.add(new Bean("手续费",
                        (String.format(Locale.CHINESE,
                                Constant.AMOUNT_FORMAT,
                                fee.doubleValue())+
                                "元")
                ));
                list.add(new Bean("交易号",
                        details.getTransaction_no()));
                list.add(new Bean("备注", details.getRemark()));
            } else {
                list.add(new Bean("交易时间", details.getCreate_time()));
                list.add(new Bean("交易类型", getChannel(details.getRate_channel_type())));
                list.add(new Bean("交易状态", convertStatus(details.getState())));
                list.add(new Bean("手续费",
                        (String.format(Locale.CHINESE,
                                Constant.AMOUNT_FORMAT,
                                fee.doubleValue())) +
                                "元")
                );
                list.add(new Bean("交易号", details.getTransaction_no()));
                list.add(new Bean("备注", details.getRemark()));
            }
        } else if (type == TransactionDetailActivity.TYPE_WITHDRAW) {
            //提现详情
            String name = details.getUser_name();
            int nameLength = name.length();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < nameLength; i++) {
                    if (i == nameLength - 1) {
                        sb.append(name.substring(i, i + 1));
                    } else {
                        sb.append("*");
                    }
                }

                list.add(new Bean("账户名称", sb.toString()));
            String debitNumber = details.getDebit_card_no();
            int cardLength=debitNumber.length();
            StringBuilder debitNumberSB = new StringBuilder(Constant.CARD_PREFIX);
            for (int i = cardLength-4; i < cardLength; i++) {
                debitNumberSB.append(debitNumber.charAt(i));
            }

            list.add(new Bean("收款账号", debitNumberSB.toString()));
            list.add(new Bean("开户银行", details.getDebit_card_name()));
            list.add(new Bean("交易时间", details.getModify_time()));
            list.add(new Bean("交易类型", getChannel(details.getRate_channel_type())));
            list.add(new Bean("手续费",
                    (String.format(Locale.CHINESE,
                            Constant.AMOUNT_FORMAT,
                            fee.doubleValue())) +
                            "元")
            );
            list.add(new Bean("交易号", details.getTransaction_no()));
            list.add(new Bean("备注", details.getRemark()));
        }
        return list;
    }

    private static String convertStatus(String status) {
        String statusStr = "";
        switch (status) {
            case "1":
                statusStr = "付款成功";
                break;
            case "2":
                statusStr = "付款失败";
                break;
            case "3":
                statusStr = "交易处理中";
                break;
        }
        return statusStr;
    }

    public static class Bean {
        private String name;
        private String value;

        public Bean(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
